﻿/// <reference path="../Scripts/MicrosoftAjax.debug.js" />

(function () {
    var scriptName = "ForgotPasswordBehavior";
    var e = null;

    function execute() {

        Type.registerNamespace("SCS");

        SCS.ForgotPasswordBehavior = function (element) {
            SCS.ForgotPasswordBehavior.initializeBase(this, [element]);

            this._stepIndex = 0;
            this._buttonClickDelegate = null;
            this._validateDelegate = null;

            this._root = null;
            this._lazyValidate = true;
        }

        SCS.ForgotPasswordBehavior.prototype = {
            initialize: function () {
                SCS.ForgotPasswordBehavior.callBaseMethod(this, 'initialize');

                this._root = $get("forgotPasswordForm");

                this._buttonClickDelegate = Function.createDelegate(this, this._onButtonClick);
                $("#proceedButton", this._root).bind("click.forgot", this._buttonClickDelegate);

                this._validateDelegate = Function.createDelegate(this, this._onValidate);
                $("input:text", this._root).bind("blur.forgot", this._validateDelegate);

                this._buildForm();
            },
            dispose: function () {
                //Add custom dispose actions here

                $("#proceedButton", this._root).unbind("click.forgot");

                $("input:text", this._root).unbind("blur.forgot");
                this._validateDelegate = null;

                SCS.ForgotPasswordBehavior.callBaseMethod(this, 'dispose');
            },
            _onButtonClick: function (e) {

                if (this._validate())
                    this._buildForm(++this._stepIndex);
            },
            _buildForm: function (index) {

                //Sys.Debug.fail("");

                var root = this._root,
                    msg = $("#dialogMessage", root),
                    cancelButton = $("#cancelButton", root),
                    proceedButton = $("#proceedButton", root),
                    step1Panel = $(".step1", root),
                    step2Panel = $(".step2", root),
                    securityQuestion = $("#securityQuestion", root);


                switch (index) {
                    case 0:
                        // assume initial rendering has correct defaults
                        break;

                    case 1:
                        this._getSecurityQuestion();
                        break;

                    case 2:
                        this._sendPassword();
                        break;
                }
            },
            _validate: function () {

                // Start validation:    
                $.validity.start();

                $("input:text:visible", this._root).require();

                // End the validation session:    
                var result = $.validity.end();

                if (!result.valid) {
                    this._lazyValidate = false;
                    return false;
                }

                return true;
            },

            _onValidate: function (e) {

                if (!this._lazyValidate) {
                    this._validate();
                }
            },

            _getSecurityQuestion: function () {

                var root = this._root,
                    data = { username: $("#usernameEntry", root).val() };

                this._callService(Sys.Serialization.JavaScriptSerializer.serialize(data), "GetSecurityQuestion");
            },

            _sendPassword: function () {

                var root = this._root,
                    data = {
                        username: $("#usernameEntry", root).val(),
                        securityAnswer: $("#securityAnswer", root).val()
                    };

                this._callService(Sys.Serialization.JavaScriptSerializer.serialize(data), "SendResetPassword");
            },

            _callService: function (json, method) {

                $.ajax({
                    type: "POST",
                    url: "AjaxService.svc/" + method,
                    contentType: "application/json; charset=utf-8",
                    success: this._onServiceCallSuccess,
                    error: this._onServiceCallError,
                    dataType: "json",
                    data: json,
                    context: this
                });
            },

            _onServiceCallError: function (xhr, status, error) {

                $("#dialogMessage", this._root).text(String.format("A \"{0}\" error has occurred. {1}", status, error));
            },

            _onServiceCallSuccess: function (data, status) {

                var root = this._root,
                    msg = null,
                    msgElement = $("#dialogMessage", root),
                    cancelButton = $("#cancelButton", root),
                    proceedButton = $("#proceedButton", root),
                    step1Panel = $(".step1", root),
                    step2Panel = $(".step2", root),
                    securityQuestion = $("#securityQuestion", root);

                if (!data.d.success) {
                    msg = data.d.message;
                    this._stepIndex = 2;
                }

                switch (this._stepIndex) {

                    case 1:
                        step2Panel.show();
                        securityQuestion.text(data.d.data);
                        msgElement.text("Answer your security question and click Finish.");
                        proceedButton.val("Finish");
                        break;

                    case 2:
                        if (!msg)
                            msg = "Your new password was emailed to you successfully."
                        msgElement.text(msg);
                        step1Panel.hide();
                        step2Panel.hide();
                        proceedButton.hide();
                        cancelButton.val("Return");
                        break;
                }
            }
        }
        SCS.ForgotPasswordBehavior.registerClass('SCS.ForgotPasswordBehavior', Sys.UI.Behavior);

        if (typeof (Sys) !== 'undefined') Sys.Application.notifyScriptLoaded();

    } // execute

    if (window.Sys && Sys.loader) {
        Sys.loader.registerScript(scriptName, null, execute);
    }
    else {
        execute();
    }

})();
